package com.pedro.encoder.input.gl.render.filters;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.pedro.encoder.R;
import com.pedro.encoder.utils.gl.GlUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * 区域虚化特效
 */

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class BlurAreaFilterRender extends BaseFilterRender {

  //rotation matrix
  private final float[] squareVertexDataFilter = {
      // X, Y, Z, U, V
      -1f, -1f, 0f, 0f, 0f, //bottom left
      1f, -1f, 0f, 1f, 0f, //bottom right
      -1f, 1f, 0f, 0f, 1f, //top left
      1f, 1f, 0f, 1f, 1f, //top right
  };

  private int program = -1;
  private int aPositionHandle = -1;
  private int aTextureHandle = -1;
  private int uMVPMatrixHandle = -1;
  private int uSTMatrixHandle = -1;
  private int uSamplerHandle = -1;

  private int uTexelSize = -1;
  private int uBlurRadius = -1;
  private int uBlurCenter = -1;
  private int uBlurRadiusArea = -1;


  //pram
  private float touchX=0.1f;
  private float touchY=0.1f;
  private float mBlurRadiusArea=0.3f;
  private float mBlurRadius=20;


  public BlurAreaFilterRender() {
    squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer();
    squareVertex.put(squareVertexDataFilter).position(0);
    Matrix.setIdentityM(MVPMatrix, 0);
    Matrix.setIdentityM(STMatrix, 0);
  }

  /**
   * uniform sampler2D uTexture;    // 输入的纹理
   * uniform vec2 uTexelSize;       // 纹理单个像素的大小
   * uniform float uBlurRadius;     // 模糊半径
   * uniform vec2 uBlurCenter;      // 虚化区域的中心点（归一化坐标）
   * uniform float uBlurRadiusArea; // 虚化区域的半径
   * @param context
   */
  @Override
  protected void initGlFilter(Context context) {
    String vertexShader = GlUtil.getStringFromRaw(context, R.raw.simple_vertex);
    String fragmentShader = GlUtil.getStringFromRaw(context, R.raw.blur_area_fragment);

    program = GlUtil.createProgram(vertexShader, fragmentShader);
    aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition");
    aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord");
    uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
    uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix");

    uTexelSize = GLES20.glGetUniformLocation(program, "uTexelSize");
    uBlurRadius = GLES20.glGetUniformLocation(program, "uBlurRadius");
    uBlurCenter = GLES20.glGetUniformLocation(program, "uBlurCenter");
    uBlurRadiusArea = GLES20.glGetUniformLocation(program, "uBlurRadiusArea");

    uSamplerHandle = GLES20.glGetUniformLocation(program, "uTexture");
  }

  @Override
  protected void drawFilter() {
    GLES20.glUseProgram(program);

    squareVertex.position(SQUARE_VERTEX_DATA_POS_OFFSET);
    GLES20.glVertexAttribPointer(aPositionHandle, 3, GLES20.GL_FLOAT, false,
        SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex);
    GLES20.glEnableVertexAttribArray(aPositionHandle);

    squareVertex.position(SQUARE_VERTEX_DATA_UV_OFFSET);
    GLES20.glVertexAttribPointer(aTextureHandle, 2, GLES20.GL_FLOAT, false,
        SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex);
    GLES20.glEnableVertexAttribArray(aTextureHandle);

    GLES20.glUniformMatrix4fv(uMVPMatrixHandle, 1, false, MVPMatrix, 0);
    GLES20.glUniformMatrix4fv(uSTMatrixHandle, 1, false, STMatrix, 0);

    // 设置模糊区域的中心点 (例如点击位置转换为归一化坐标)
    GLES20.glUniform2f(uBlurCenter, touchX/getWidth(), touchY/getHeight() );

// 设置模糊区域的半径
    GLES20.glUniform1f(uBlurRadiusArea, mBlurRadiusArea); // 半径为 30% 的屏幕宽度

// 设置模糊强度
    GLES20.glUniform1f(uBlurRadius, mBlurRadius); // 模糊半径为 50个像素,最大值50，屏幕角落最大10

    GLES20. glUniform2f(uTexelSize, 1.0f/getWidth(), 1.0f/getHeight());

    GLES20.glUniform1i(uSamplerHandle, 4);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE4);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, previousTexId);
  }

  @Override
  public void release() {
    GLES20.glDeleteProgram(program);
  }

  public void setTouchXY(float x,float y){

    this.touchX= x;
    this.touchY= y;
  }

  /**
   * 取值1-100
   * @param blurRadiusArea
   */
  public void setBlurRadiusArea(int blurRadiusArea){
    this.mBlurRadiusArea= (float) blurRadiusArea /100;
  }

  /**
   * 1-50
   * @param pBlurRadius
   */
  public void setBlurRadius(int pBlurRadius){
    this.mBlurRadius= (float) pBlurRadius;
  }

}
